package com.fubang.pathlearning.path.radar;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import static android.R.attr.path;

/**
 * 创建者: YIN
 * 创建时间: 2017/7/5 15:55
 * 电子邮箱: yin_ruimin@foxmail.com
 * 描述:雷达图
 * 1.获得位置中心
 * 2.绘制蜘蛛网络
 * 3.绘制中心连线
 * 4.绘制文本
 * 5.绘制覆盖区域
 * <p>
 * 涉及数值:标题名称，数值，最大数值，蜘蛛网颜色，数值颜色，覆盖区域颜色
 */

public class RadarView extends View {
    private int mWidth;
    private int mHeight;
    private Paint mTextPaint;//文本画笔
    private Paint mLinePaint;//雷达线条画笔
    private Paint mRegionPaint;//覆盖区域画笔
    private int count = 6;//数据个数
    private String[] titles = {"A", "B", "C", "D", "E", "F"};//标题名称
    private int maxValue = 100;//最大数值
    private String[] data = {"77", "62", "56", "82", "55", "100"};//标题数值
    private float radius;//区域半径
    private float angle = (float) (Math.PI * 2 / count);//根据不同的维度个数每个维度显示的角度

    public RadarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        radius = Math.min(w, h) / 2 * 0.8f;
        this.mWidth = w;
        this.mHeight = h;
    }

    private void init() {
        mTextPaint = new Paint();
        mLinePaint = new Paint();
        mRegionPaint = new Paint();

        mTextPaint.setStrokeWidth(4f);
        mTextPaint.setStyle(Paint.Style.FILL);
        mTextPaint.setColor(Color.RED);
        mTextPaint.setTextSize(40);

        mLinePaint.setStrokeWidth(4f);
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setColor(Color.BLACK);

        mRegionPaint.setStrokeWidth(4f);
        mRegionPaint.setStyle(Paint.Style.FILL);
        mRegionPaint.setColor(Color.BLUE);
        mRegionPaint.setAntiAlias(true);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(mWidth / 2, mHeight / 2);//将画布中心从当前view的左上角转移到view的中心，方便绘制
//        canvas.scale(1, -1);//翻转y轴，但是带来的弊端就是字体也轴对称了
        drawPolygon(canvas);//绘制多边形
        drawLines(canvas);//绘制中心到端点连线
        drawText(canvas);//绘制文字
        drawRegion(canvas);//绘制覆盖区域
    }


    private void drawPolygon(Canvas canvas) {
        Path path = new Path();
        float r = radius / (count - 1);//最小间隔
        for (int i = 1; i < count; i++) {//此循环为了求得多边形间隔
            float currentR = r * i;//每次的半径
            path.reset();
            for (int j = 0; j < count; j++) {
                if (j == 0) {
                    path.moveTo(currentR, 0);//移动到起始点
                } else {
                    float x = (float) (currentR * Math.cos(angle * j));//顺时针想x周作垂线，夹角三角函数
                    float y = (float) (currentR * Math.sin(angle * j));
                    path.lineTo(x, y);
                }
            }
            path.close();
            canvas.drawPath(path, mLinePaint);//在第一个循环中绘制闭合路线，同时将path重置
        }

    }


    private void drawLines(Canvas canvas) {
        Path path = new Path();
        for (int i = 0; i < count; i++) {
            path.reset();
            path.moveTo(0, 0);
            float x = (float) (radius * Math.cos(angle * i));//顺时针想x周作垂线，夹角三角函数
            float y = (float) (radius * Math.sin(angle * i));
            path.lineTo(x, y);
            canvas.drawPath(path, mLinePaint);
        }
    }


    private void drawText(Canvas canvas) {
        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        float fontHeight = fontMetrics.descent - fontMetrics.ascent;
        for (int i = 0; i < count; i++) {
            float x = (float) ((radius + fontHeight / 2) * Math.cos(angle * i));
            float y = (float) ((radius + fontHeight / 2) * Math.sin(angle * i));
            if (angle * i >= 0 && angle * i <= Math.PI / 2) {//第4象限
                canvas.drawText(titles[i], x, y, mTextPaint);
            } else if (angle * i >= 3 * Math.PI / 2 && angle * i <= Math.PI * 2) {//第3象限
                canvas.drawText(titles[i], x, y, mTextPaint);
            } else if (angle * i > Math.PI / 2 && angle * i <= Math.PI) {//第2象限
                float dis = mTextPaint.measureText(titles[i]);//文本长度
                canvas.drawText(titles[i], x - dis, y, mTextPaint);
            } else if (angle * i >= Math.PI && angle * i < 3 * Math.PI / 2) {//第1象限
                float dis = mTextPaint.measureText(titles[i]);//文本长度
                canvas.drawText(titles[i], x - dis, y, mTextPaint);
            }
        }
    }

    private void drawRegion(Canvas canvas) {
        Path path = new Path();
        for (int i = 0; i < count; i++) {
            double percent = Double.parseDouble(data[i]) / maxValue;
            float x = (float) (radius * Math.cos(angle * i) * percent);//顺时针想x周作垂线，夹角三角函数
            float y = (float) (radius * Math.sin(angle * i) * percent);
            if (i == 0) {
                path.moveTo(x, 0);
            } else {
                path.lineTo(x, y);
            }
            canvas.drawCircle(x, y, 10, mTextPaint);
        }
        path.close();
        mRegionPaint.setAlpha(127);
        canvas.drawPath(path, mRegionPaint);
    }
}
